layui.use(['laydate','form','table'], function(){
	var laydate = layui.laydate;
	var form = layui.form;
	var table = layui.table;

	/*开始时间*/
	laydate.render({
	  	elem: '#start' //指定元素
	});
	/*结束时间*/
	laydate.render({
	  	elem: '#end' //指定元素
	});
	/*@初始化table*/
	var tableObj = table.render({
	    elem: '#table-list',
	    url: '/user/user/rank', //数据接口
	    page: true, //开启分页
	    limit: 5,
	    cols: [[ //表头
	    	{field: 'id', title: '排行'},
	    	{field: 'user_sn', title: '客户编号'},
	    	{title: '照片', toolbar: '<div><img src="{{d.user_img}}" width="50" height="50"></div>'},
	    	{field: 'username', title: '姓名'},
	    	{field: 'nkname', title: '称呼'},
	    	{field: 'sex', title: '性别'},
	    	{field: 'age', title: '年龄'},
	    	{field: 'phone', title: '联系方式'},
	    	{field: 'spare_phone', title: '备用联系方式'},
	    	{field: 'u_type', title: '客户类型'},
	    	{field: 'number', title: '来店次数'},
	    	{field: 'visit', title: '拜访时间'},
	    	{field: 'order_money', title: '购买金额'},
	    	{field: 'buy_time', title: '最近购买时间'},
	    	{title: '订单记录', width: 90, toolbar: '<div><a class="layui-btn" onclick="xadmin.open(\'订单记录\',\'/user/order/index/user_sn/{{d.user_sn}}\')">订单记录</a></div>'},
	    	{title: '咨询记录', width: 90, toolbar: '<div><a class="layui-btn" onclick="xadmin.open(\'咨询记录\',\'/user/consult/index/user_sn/{{d.user_sn}}\')">咨询记录</a></div>'},
	    	{title: '维修记录', width: 90, toolbar: '<div><a class="layui-btn" onclick="xadmin.open(\'维修记录\',\'/user/repair/index/user_sn/{{d.user_sn}}\')">维修记录</a></div>'},
	    	{title: '操作', width: 100,toolbar: '<div><a class="layui-btn" onclick="xadmin.open(\'查看详情\',\'/user/user/edit/ids/{{d.user_id}}\')">查看详情</a></div>'},
	    ]],
	    parseData: function(res){
	    	if(res.code === 0){
                var num = (res.page-1) * res.row
                for(var i = 0;i < res.data.length;++i){
                    res.data[i].id = num + i + 1
                }
                exportData = res.data
            }
	    },
	});
	/*@查询*/
	form.on('submit(search)',function(data){

		if(data.field.start != '' && data.field.end != ''){
			var startDate = new Date(data.field.start).getTime();
			var endDate = new Date(data.field.end).getTime();
			if(startDate > endDate){
				layer.msg('开始时间不能大于结束时间',{icon: 2})
				return false
			}
		}
		/*重载表格数据*/
		tableObj.reload({
			page: {
                page: 1 //重新从第 1 页开始
            },
            where: data.field
        });
        return false
	})
});